#ifndef DODO_WAVE_PLOT_H
#define DODO_WAVE_PLOT_H
#include <string>
#include <iostream>
#include <vector>
#include <cairomm/cairommconfig.h>
#include <cairomm/context.h>
#include <cairomm/surface.h>

void WavePlot(const std::vector<double> & table, bool headTail = false, const std::string& outFileName = "wave.png")
{
	Cairo::RefPtr<Cairo::ImageSurface> surface =
			Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, 800, 600);
	Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(surface);

    double picHeight = surface->get_height();
    double picWidth = surface->get_width();
    double frameMinHeight = picHeight * 0.08;
    double frameMaxHeight = picHeight * 0.92;
    double frameMinWidth = picWidth * 0.08;
    double frameMaxWidth = picWidth * 0.92;

    int gridNum = table.size();
    double gridWidth = (frameMaxWidth - frameMinWidth) / gridNum;

    std::cout<<frameMinHeight <<"\t" <<frameMaxHeight <<"\t"
        <<frameMinWidth <<"\t" <<frameMaxWidth<<"\t"
        <<gridNum<<"\t"<< gridWidth<<std::endl;

	cr->save();
	cr->set_source_rgb(0.86, 0.85, 0.47);
    cr->paint();    // fill image with the color
    cr->restore();  // color is back to black now

    cr->save();

        int shift = 0;
    if( headTail)
    {
        shift = gridNum/2;
    }

    cr->set_line_width(1.0);

    cr->set_source_rgb(0,0,0);
	for( int i = 0; i< gridNum; ++ i)
	{
        cr->rectangle(frameMinWidth+ i*gridWidth ,
            frameMaxHeight,
            gridWidth,
            - table[(i+shift) % gridNum] * (frameMaxHeight-frameMinHeight) );
        cr->stroke();
	}

    cr->set_line_width(0.5);
	if( headTail)
	{
	    cr->set_source_rgba(225,0, 0, 0.7);
	    double lineXPos = frameMinWidth+shift*gridWidth;
        cr->move_to( lineXPos , frameMaxHeight);
        cr->line_to( lineXPos , frameMinHeight);
        cr->stroke();
	}

	//0line and frame
	cr->set_source_rgba(0,0,30,0.8);
	cr->move_to(frameMinWidth, (frameMaxHeight-frameMinHeight)*0.5 + frameMinHeight);
	cr->line_to(frameMaxWidth,  (frameMaxHeight-frameMinHeight)*0.5 + frameMinHeight);
    cr->stroke();
	cr->rectangle(frameMinWidth, frameMinHeight,
        frameMaxWidth-frameMinWidth, frameMaxHeight-frameMinHeight);
    cr->stroke();
    surface->write_to_png(outFileName);

}
#endif

